package com.czc.pay.controllers;

import javax.servlet.http.HttpServletRequest;

import com.citywithincity.db.EntityBuilder;
import com.citywithincity.db.TableBindInfo;
import com.damai.annotation.Controller;
import com.damai.error.ApiError;
import com.jzoom.zoom.pay.PayChannel;
import com.jzoom.zoom.pay.PayException;
import com.jzoom.zoom.pay.PayManager;
import com.jzoom.zoom.pay.alipay.AlipayAdapter;
import com.jzoom.zoom.pay.alipay.AlipayConfig;
import com.jzoom.zoom.pay.models.CreatePay;
import com.jzoom.zoom.pay.models.Pay;

@Controller(key = "test_pay")
public class PayController {

static AlipayConfig config = new AlipayConfig();
	
	static {
		config.setPartner("2088411752388544");
		config.setSeller("etongka123@163.com");
		config.setPrivateKey("MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAMzUyV0AbVwPXizju4dTNRRM0FlEMSAA5lw3QbA+TuEeX/epYDXW84QTveQmbGaawXqqcY9tMuh+tEuyMrvraxzoRRJwEpT1K3C+Gd4/fFvHBEXkyr/AUd06IaYWw4LEKg08016pmy8CQE23xBWqHd3tKlAKHpuWiIDb6sd2Q1BNAgMBAAECgYBAyXkbcnwkpzO5bRqulfHvBtDLu1bycXZraorbbQnUCKCAHFGzFdcWSoxAcqrM1VwnKWwb2oVEpnbujmGfsLnC7RSXNL6l8d/VKwjp8Rs2ZcRNuGqKnnZGM2INZO5M3EIWG11Ed9RY202HdCz+xmJIXw4J2Fx1Ndk7TOOfxhoiQQJBAOUEdKIr9DYdLDNULyDYgmxbeIKF3nkSCf0qpBpdQ5XOsGDWjkQjadcG9NHId4f8IHhT8KjxCZcduG+T1yx79HECQQDk9tW7jQJHacRp/qRQNd4PR5QI3YLmlp/BgBTbUWP24cx1lyCh0udSDBNifQ6cbn9VHjAsbZW5PWU3IywW7FedAkAEuXrOfamkFCX9SRdPzieipKBv0OMuDDk4h9Cq2pGyXXk2BjcZIMHmNbaHPWN54rGoydKFKbPQ8kQDyUD/Ie+BAkAbNk7urHKh5Cso4c/LjKtoq3wnlZQSLcKs77kK2+CCnhF7xEtnkDZX5QGEyhIre2J8zkqxbRDHZEmcRPzfeJVtAkBHr5/QtqvUO2qXVNwdNq6dwXKR3vsvFhWrjn7z7rtuvNpAtrUSRn78qHznMY1pIHGuuxiJin5PTMdd41k4E+l6");
		config.setSafeKey("5agw2hrhx7mtj7dp5kanoguj3bj1qgup");
		config.setPublicKey("MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB");
	
		PayManager.me().setPayNotifyUrl("http://127.0.0.1/");
	}
	
	public PayController() {
		TableBindInfo bindInfo = new TableBindInfo("sys_pay", 0, "PY_ID", true);
		EntityBuilder.bindEntity(  Pay.class,bindInfo );
		PayManager.me().registerPayAdapter("001","alipay", new AlipayAdapter(config));
	}
	
	/**
	 * 创建订单
	 */
	public Pay create(HttpServletRequest request,int fee,String title) {
		try {
			CreatePay json = new CreatePay();
			json.setClientIp(request.getRemoteAddr());
			json.setAmount(fee);
			json.setTitle(title);
			json.setMchId("001");
			json.setOrderNo( String.valueOf(System.currentTimeMillis()) );
			json.setChannel(PayChannel.alipay);
			
			return PayManager.me().create(json);
		}catch (PayException e) {
			throw new ApiError(e.getError());
		}
		
	}
	
}
